import os, music_tag
from uuid import uuid4

from data import config, database

def scrape():
    files_skipped = 0
    updated_titles = 0
    updated_albums = 0
    updated_artists = 0
    updated_genres = 0
    print('Scraping existing files for missing metadata')
    for uuid in database['music']:
        track = database['music'][uuid]
        if track['title'] and track['album'] and track['artists'] and track['genre'] and (database['albums'][track['album']]['artist'] if track['album'] else True):
            files_skipped += 1
            continue
        print('Scraping %s' % uuid)
        path = os.path.join(config['files']['audio']['path'], track['path'])
        file = music_tag.load_file(path)
        if not track['title'] and file['title'].value:
            track['title'] = file['title'].value
            updated_titles += 1
        if not track['album'] and file['album'].value:
            for uuid in database['albums']:
                album = database['albums'][uuid]
                if file['album'].value == album['name']:
                    track['album'] = uuid
                    break
            else:
                uuid = uuid4()
                album_artist = None
                if file['albumartist']:
                    for uuid in database['artists']:
                        artist = database['artists'][uuid]
                        if file['albumartist'] == artist['name']:
                            album_artist = uuid
                            break
                    else:
                        uuid = uuid4()
                        database['artists'][uuid] = {
                            'name': file['albumartist'].value
                        }
                        album_artist = uuid
                database['albums'][uuid] = {
                    'name': file['album'].value,
                    'artist': album_artist,
                    'cover': None
                }
                track['album'] = uuid
            updated_albums += 1
        if not track['artists'] and file['artist'].value:
            artists = []
            for delimeter in config['delimeters']:
                split_str = file['artist'].value.split(delimeter)
                if len(split_str) > 1:
                    artists = split_str
                    break
            else:
                artists = [file['artist'].value]
            for artist in artists:
                artist = artist.strip()
                for uuid in database['artists']:
                    db_artist = database['artists'][uuid]
                    if artist == db_artist['name']:
                        track['artists'].append(uuid)
                        break
                else:
                    uuid = uuid4()
                    database['artists'][uuid] = {
                        'name': artist
                    }
                    track['artists'].append(uuid)
            track['artists'] = list(set(track['artists']))
            updated_artists += 1
        if not track['genre'] and file['genre'].value:
            for uuid in database['genres']:
                genre = database['genres'][uuid]
                if file['genre'].value == genre['name']:
                    track['genre'] = uuid
                    break
            else:
                uuid = uuid4()
                database['genres'][uuid] = {
                    'name': file['genre'].value
                }
                track['genre'] = uuid
            updated_genres += 1

    print('Skipped %s files' % files_skipped)
    print('%s files had there titles updated' % updated_titles)
    print('%s files had there associated albums updated' % updated_albums)
    print('%s files had there contributing artists updated' % updated_artists)
    print('%s files had there genres updated' % updated_artists)
